﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using ESRI.ArcGIS.Geometry;

namespace popGis
{
    class DistributionCenter
    {
        /// <summary>
        /// 计算人口分布中心
        /// </summary>
        /// <param name="shapeCenter">可显式转换为ShapeCenter数组的ArrayList</param>
        /// <returns></returns>
        static public IPoint CalculateDistributionCenter(ArrayList shapeCenter)
        {
            double x = 0; double y = 0; double totalpop = 0;
            
            foreach (ShapeCenter shpCenter in shapeCenter)
            {
                x += shpCenter.Center.X * shpCenter.Pop;
                y += shpCenter.Center.Y * shpCenter.Pop;
                totalpop += shpCenter.Pop;
            }

            if (totalpop == 0) return null;

            x = x / totalpop; y = y / totalpop;
            IPoint popCenter = ConstructPoint2D(x, y);

            return popCenter;
        }
        
        /// <summary>
        /// 依据XY构造2维IPoint对象
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static IPoint ConstructPoint2D(double x, double y)
        {
            IPoint point = new PointClass();
            point.PutCoords(x, y);
            return point;
        }
    }
}
